package io.github.handyang.algorithms.graph;

import io.github.handyang.algorithms.Bag;

/**
 * 邻接数组
 */
public class ArrayGraph implements Graph {
    private final Bag[] adj;

    private int E;

    public ArrayGraph(int V) {
        adj = new Bag[V];
        for (int i = 0; i < adj.length; i++) {
            adj[i] = new Bag();
        }
    }

    @Override
    public int V() {
        return adj.length;
    }

    @Override
    public int E() {
        return E;
    }

    @Override
    public void addEdge(int v, int w) {
        adj[v].add(w);
        adj[w].add(v);
        E++;
    }

    @Override
    public Iterable<Integer> adj(int v) {
        return adj[v];
    }

    @Override
    public String toString() {
        return "ArrayGraph{}";
    }
}
